# vim: ts=4:sw=4:syntax=perl

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_user_name redirect_header);
use gri_user_alert_config;
use File::Basename;
use xml_helper;
use Data::Dumper;
use gri2_requests;

sub mm_new_grant_portal_access_process {
my $_aa=new xml_helper();

	# We can allow access via this mechanism in either of the
	# following cases (access which should have been checked before getting here..).
	# 1. We are one of the owners of the portal
	# 2. Have general portal_admin access
	# Otherwise we just

	my $user_name=get_user_name();
	my $url_prefix=$::GRI_FRONTEND->get_url_prefix();
	my $portal=$::PARAM{portal};
	my $user=$::PARAM{user};
	my $allow=$::PARAM{allow};
	$allow="" if !defined($allow);
	my $pstr=hex_to_string($portal);
	my $ustr=hex_to_string($user);
	my $dd=$::GRI_FRONTEND->get_portal_details($pstr);
	if(defined($dd->{error})) {
		my $serror=$dd->{error};
		my $error="Failed to get portal details.";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}
	my $req_dir=$::GRI_FRONTEND->get_directory("requests");
	if(!defined($req_dir)) {
		my $error="Failed to find 'requests' directory.";
		my $serror="";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}
	my $link=link_to_str("stdlink","here","_","");
	# We are owner or portal admin, so close any requests if necessary.
	# If an owner we must update the request record (if any)
	my $user_s=hex_to_string($user);
	my $portal_s=hex_to_string($portal);
			
	my $rdb=new gri2_requests("$req_dir/requests.db");
	if(!defined($rdb)) {
		my ($rc,$str);
		($rc,$str)=gri2_requests::error();
		my $error="Failed to open requests database.";
		my $serror=$str;
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}
	my $err=$rdb->grant_portal_access_request({user => $user_s, portal => $portal_s, text => $allow,
				frontend => $::GRI_FRONTEND, who_by =>$user_name,
	});

	if(defined($err->{error})) {
		my $serror=$err->{error};
		my $error="Failed to grant access request.";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}

	load_macro('mm_start_page');
	load_macro('mm_header');
	mm_start_page();
	my $s=mm_header(undef,undef,1);

	if(defined($err->{id}) && $err->{id} eq 'not_found') {
		return $s . html_error("No such user request has been found!");
	}
	if($err->{allow}==-1) {
		return $s . "Your decision has been logged; though not enough votes have been casted as yet to provide a decision to the user.<p>Click $link to return to the home page.";
	}
	if($err->{allow}==1) {
		# So any records have been updated if necessary, so next inform user by email...
		my $email=$::GRI_FRONTEND->get_user_email_address($user_s);
		if(!defined($email)) {
			return $s . html_error("Failed to get email address for '$user_s'.",1);
		}
		my $txt="<font style='font-weight: bold;'>Your access request to portal '$portal_s' has been allowed.";
	
		my $from=$::GRI_FRONTEND->get_email_sent_from_address();
		my $subject="Access request to $portal_s - GRANTED";
		my $cc='';

		my ($rc2,$err2);
		($rc2,$err2)=$::GRI_FRONTEND->send_email({
			html=>1, subject=>$subject, to=>$email, from=>$from, content=>$txt
		});

		# Indicate an email has been sent to the user and any requests
		# have been closed in denied status.

		my ($rc3,$err3)=$::GRI_FRONTEND->add_portal_users($portal_s,[$user_s]);
		if($rc3==0) {
			return $s . html_error("Failed to update portal! $err3",1);
		}
		return $s . "Your decision allowed the request to be allowed. The user will be informed.<p>Click $link to return to the home page.";
	}
	{
		# Must be here if access has been denied
		my $email=$::GRI_FRONTEND->get_user_email_address($user_s);
		if(!defined($email)) {
			return $s . html_error("Failed to get email address for '$user_s'.",1);
		}
		my $txt="<font style='font-weight: bold;'>Your access request to portal '$portal_s' has been denied.";
	
		my $from=$::GRI_FRONTEND->get_email_sent_from_address();
		my $subject="Access request to $portal_s - DENIED";
		my $cc='';

		my ($rc2,$err2);
		($rc2,$err2)=$::GRI_FRONTEND->send_email({
			html=>1, subject=>$subject, to=>$email, from=>$from, content=>$txt
		});

		# Indicate an email has been sent to the user and any requests
		# have been closed in denied status.

		return $s . "Your decision allowed the request to be DENIED. The user will be informed.<p>Click $link to return to the home page.";
	}
}

1;
